package com.atbun.eduservice.service.impl;

import com.atbun.eduservice.entity.EduCourse;
import com.atbun.eduservice.entity.EduCourseDescription;
import com.atbun.eduservice.entity.front.CourseQueryVo;
import com.atbun.eduservice.entity.front.CourseWebVo;
import com.atbun.eduservice.entity.vo.CourseInfoVo;
import com.atbun.eduservice.entity.vo.CoursePublishVo;
import com.atbun.eduservice.entity.vo.CourseQuery;
import com.atbun.eduservice.mapper.EduCourseMapper;
import com.atbun.eduservice.service.EduChapterService;
import com.atbun.eduservice.service.EduCourseDescriptionService;
import com.atbun.eduservice.service.EduCourseService;
import com.atbun.eduservice.service.EduVideoService;
import com.atbun.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-06
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduCourseMapper eduCourseMapper;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    //新增课程
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
//        保存需要使用实体类
        EduCourse eduCourse = new EduCourse();
        eduCourse.setStatus(EduCourse.COURSE_DRAFT);
//        把vo的数据复制到实体中
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        boolean resultCourseInfo = this.save(eduCourse);
        if (!resultCourseInfo){
            throw new GuliException(20001, "课程信息保存失败");
        }

//        保存课程简介信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(eduCourse.getId());// 把课程id设置进课程详情
        boolean resultDescription = this.eduCourseDescriptionService.save(eduCourseDescription);
        if(!resultDescription){
            throw new GuliException(20001, "课程详情信息保存失败");
        }

        return eduCourse.getId();
    }

    /**
     * 根据课程id查询课程信息
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoVo getCourseInfoFormById(String courseId) {
//        查询课程表
        EduCourse eduCourse = this.baseMapper.selectById(courseId);
        if (null == eduCourse){
            throw new GuliException(20001, "该课程不存在");
        }
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

//        查询描述表
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        if(eduCourseDescription != null){
            courseInfoVo.setDescription(eduCourseDescription.getDescription());
        }
        return courseInfoVo;
    }

    /**
     * 修改课程信息
     * @param courseInfoVo
     * @return
     */
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
//        先修改课程信息表
        if (null == courseInfoVo){
            throw new GuliException(20001, "该课程不存在");
        }
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse); //第一个参数是有值的，第二个是没有值的
        int updateById = this.baseMapper.updateById(eduCourse);
        if (updateById == 0){
            throw new GuliException(20001,"修改课程信息失败");
        }

//        修改课程描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }
    //*************************************推送课程信息 开始**************************************//
    /**
     * 根据课程id查询课程详情，课程科目，讲师
     * @param id
     */
    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {
        CoursePublishVo coursePublishVo = this.eduCourseMapper.getCoursePublishVoById(id);
        if (null == coursePublishVo){
            throw new GuliException(20001,"请重新检查所发布的课程信息");
        }
        return coursePublishVo;
    }

    /**
     * 发布课程信息
     * @param id
     */
    @Override
    public boolean publishCourseById(String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus(EduCourse.COURSE_NORMAL);
        Integer count = this.baseMapper.updateById(eduCourse);
        return null != count && count > 0;
    }
    //*************************************推送课程信息 结束**************************************//

    //*************************************课程列表信息 开始**************************************//
    /**
     * 分页查询课程列表
     * @param eduCoursePage
     * @param courseQuery
     */
    @Override
    public void getCoursePageList(Page<EduCourse> eduCoursePage, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> qw = new QueryWrapper<>();
//        课程名称模糊查询
        qw.like(StringUtils.isNotBlank(courseQuery.getTitle()),"title",courseQuery.getTitle());
//        讲师id精确查询
        qw.eq(courseQuery.getTeacherId() != null,"teacher_id",courseQuery.getTeacherId());
//        大于 一级类别 小于等于二级类别
        qw.ge(courseQuery.getSubjectParentId() != null,"subject_parent_id",courseQuery.getSubjectParentId());
        qw.le(courseQuery.getSubjectId() != null, "subject_id",courseQuery.getSubjectId());
        qw.orderByDesc("gmt_create");
        this.baseMapper.selectPage(eduCoursePage,qw);
    }

    /**
     * 根据课程id删除课程，章节-小节，视频
     * @param courseId
     * @return
     */
    @Override
    public boolean deleteCourseAndTeacherAndVideoByCourseId(String courseId) {

//        删除章节-小节
        this.eduChapterService.deleteChapterByCourseId(courseId);
//        删除视频
//        this.eduVideoService.deleteVideoByCourseId(courseId);
        this.eduVideoService.removeByCourseId(courseId);
//        删除课程信息
        boolean deleteCourseById = this.deleteCourseById(courseId);
        return deleteCourseById;
    }

    /**
     * 根据课程id删除课程
     * @param id
     * @return
     */
    @Override
    public boolean deleteCourseById(String id) {
        Integer deleteCourse = baseMapper.deleteById(id);
        return null != deleteCourse &&deleteCourse > 0;
    }

    //*************************************课程列表信息 结束**************************************//

//    *************************************客户端 开始***********************************************//
    /**
     * 根据讲师id查询课程信息
     * @param teacherId
     * @return
     */
    @Override
    public List<EduCourse> queryCourseByTeacherId(String teacherId) {
        QueryWrapper<EduCourse> queryCourse = new QueryWrapper<>();
        queryCourse.eq("teacher_id",teacherId);
        queryCourse.orderByDesc("gmt_modified");
        List<EduCourse> eduCourses = this.baseMapper.selectList(queryCourse);
        return eduCourses;
    }

    @Override
    public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(courseQuery.getTitle())) {
            queryWrapper.eq("title", courseQuery.getTitle());
        }

        if (!StringUtils.isEmpty(courseQuery.getTeacherId())) {
            queryWrapper.eq("teacher_id", courseQuery.getTeacherId());
        }

        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
        }

        if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
            queryWrapper.eq("subject_id", courseQuery.getSubjectId());
        }

        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {
            queryWrapper.orderByDesc("buy_count");
        }

        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam, queryWrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    /**
     * 根据课程id查询课程详情
     * @param id
     * @return
     */
    @Override
    public CourseWebVo selectInfoWebById(String id) {
//        先修改访问量+1
        this.updatePageViewCount(id);
//        查询课程详情
        CourseWebVo courseWebVo = this.baseMapper.selectInfoWebById(id);
        return courseWebVo;
    }

    /**
     * 每次点击访问量自动加1
     * @param id
     */
    @Override
    public void updatePageViewCount(String id) {
        EduCourse eduCourse = this.baseMapper.selectById(id);
        eduCourse.setViewCount(eduCourse.getViewCount() + 1);
        baseMapper.updateById(eduCourse);
    }

//    *************************************客户端 结束***********************************************//

}
